# 自定义gdb命令 create_cstr_with_len_then_print:  复制来源字符串开头的给定长度字符串 并打印

#{ 命令 create_cstr_with_len_then_print 定义开始
define create_cstr_with_len_then_print


#假while循环 开始
set var $loop_cond = 1
while $loop_cond

set $_srcCStr=$arg0
set $_copyLength=$arg1
set $_title=$arg2

if $_srcCStr==0 || $_copyLength==0
loop_break
#提前退出 本命令过程 用 loop_break
end
#if结束

# ptype snprintf
# type = int (char *, size_t, const char *, ...)

	#https://unix.stackexchange.com/a/151609,  https://unix.stackexchange.com/questions/151502/how-to-save-the-result-of-printf-to-a-variable-in-gdb
	#https://stackoverflow.com/a/7899144, https://stackoverflow.com/questions/7899119/what-does-s-mean-in-printf
	#https://stackoverflow.com/a/2239571, https://stackoverflow.com/questions/2239519/is-there-a-way-to-specify-how-many-characters-of-a-string-to-print-out-using-pri
# eval "set $MI_Token_cStr = \"%.*s\"", $bufferDataCStr+$name_tokenIdx
# eval "set $MIRepTk_Token_cStr = \"%.*s\"", $bufferDataCStr+$valueToken0_tokenIdx,$valueToken0Ptr->getLength ()
#gdb脚本不支持 而 报错: `*' not supported for precision or width in printf
			

set $_copyCStr=(char*)malloc(1+$_copyLength)
#call snprintf(...) 总是会打印返回值, 因此用 set $无用变量=snprintf(...)代替 不
set $_ignoreMe= snprintf($_copyCStr,1+$_copyLength,"%.*s",$_copyLength,$_srcCStr)
#用snprintf代替了strncpy
# call $_copyCStr[$_copyLength]='\0'
# call strncpy($_copyCStr,$_srcCStr,$_copyLength)
printf "%s%s\n",$_title,$_copyCStr
call free($_copyCStr)



# loop_break
#退出假while循环

set var $loop_cond = 0
end
#假while循环 结束

end
#命令 Twine_union_Child_print 定义结束 }
